import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

// 创建线程的方式
public class jucThread {
    public static void main(String[] args) {
        new Thread(new RunnableThread(),"R1").start();
        // Thread() 不直接支持 Callable
        // FutureTask 作为中间人
        FutureTask<Integer> task = new FutureTask<Integer>(new CallableThread());
        // lamda 可以简化，不需要增加额外类
        // 需要函数接口才可以使用 @FunctionalInterface
        FutureTask<String> task2 = new FutureTask<>(()->{
            System.out.println(Thread.currentThread().getName()+" running");
            Thread.sleep(2000);
            return "200";
        });
        // FutureTask 只是一个任务
        new Thread(task,"C1").start();
        new Thread(task2,"C2").start();
        // FutureTask 的 get 其实是阻塞的
        try {
            System.out.println(task2.get());
        } catch (InterruptedException e) {
            
            e.printStackTrace();
        } catch (ExecutionException e) {
            
            e.printStackTrace();
        }
        
    }
}
class CallableThread implements Callable{

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName());
        return 200;
    }
    
}
class RunnableThread implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }

}